<?php
/**
 * Show and manage the comments feeds
 *
 * @author University of London Computer Centre
 * @version 1.3.0 - Release Candidate 1
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 * @global ArchivePressLog $ArchivePressLog
 */

// globals
global $ArchivePress;
global $ArchivePressLog;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php&amp;back=edit-comments';


// get info to apply the filters
// STATUS (all or only active feeds)
$status = "";
if(isset($_GET["post_status"])){
    $status = attribute_escape($_GET["post_status"]);
}

// SEARCH FIELD
$search = "";
if(isset($_GET["s"])){
    $search = stripslashes($_GET["s"]);
}

// PROTOCOL LIST
$protocol = "";
if(isset($_GET["protocol"])){
    $protocol = attribute_escape($_GET["protocol"]);
    if($protocol == "ALL"){
        $protocol = "";
    }
}

// BLOGS LIST
$parentblog = "";
if(isset($_GET["parentblog"])){
    $parentblog = attribute_escape($_GET["parentblog"]);
    if($parentblog == "ALL"){
        $parentblog = "";
    }
}
$optionBlogs = "";
$blogs = $ArchivePress->getFeeds(false, false, "", "", "", "yes");
foreach($blogs as $blog) {
    $optionBlogs .= '<option value="'.$blog["id"].'"'; if($blog["id"] == $parentblog) $optionBlogs .= ' selected="selected"'; $optionBlogs .='>'.$ArchivePress->cleanOutput($blog["name"]).'</option>';
}


// CHECK FOR MESSAGES
$message = "";
// active
if(array_key_exists("active", $_GET)){
    if((int)$_GET["active"] == 1){
        // SINGLE ACTION
        $message = sprintf(__("Feed Activate. <a href='%s'>Undo</a>",ARCHIVEPRESS), wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deactivatecommentfeed&amp;feed_id='.(int)$_GET["feed_id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]));
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Activate",ARCHIVEPRESS), (int)$_GET["active"]);
    }
}

// inactive
if(array_key_exists("inactive", $_GET)){
    if((int)$_GET["inactive"] == 1){
        // SINGLE ACTION
        $message = sprintf(__("Feed Deactivate. <a href='%s'>Undo</a>",ARCHIVEPRESS), wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=activatecommentfeed&amp;feed_id='.(int)$_GET["feed_id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]));
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Deactivate",ARCHIVEPRESS), (int)$_GET["inactive"]);
    }
}

// update
if(array_key_exists("fetchedfeed", $_GET)){
    $feed = $ArchivePress->getFeed((int)$_GET["feed_id"]);
    $message = sprintf(__("Fetched feed %s (%s)> comments added: %d ",ARCHIVEPRESS), $ArchivePress->cleanOutput($feed["name"]), $ArchivePress->cleanOutput($feed["url"]), (int)$_GET["feeds"]);
}

if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p>
                        '.$message.'<br />
                    </p>
                </div>';
}

// LOADING gif
echo '<div id="arpr_loadinggif"><img src="../wp-content/plugins/archivepress/images/ajax-loader.gif" alt="" /></div>';

echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("Edit Comment Feeds", ARCHIVEPRESS).'
            <a class="button add-new-h2" href="admin.php?page=archivepress/pages/add-feed.php">'.__("Add New Feed", ARCHIVEPRESS).'</a>
        </h2>

        '.$message.'

        <form method="get" action="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=show_commentfeeds', WP_ARCHIVEPRESS_PREFIX . 'show_commentfeeds').'" id="posts-filter">
        <input type="hidden" name="page" value="archivepress/pages/responder.php" />
        <input type="hidden" name="'.WP_ARCHIVEPRESS_PREFIX.'action" value="show_commentfeeds" />
        <input type="hidden" name="back" value="edit-comments" />
        <input type="hidden" name="_wpnonce" value="'.wp_create_nonce(WP_ARCHIVEPRESS_PREFIX . 'show_commentfeeds').'" />';


// total and active feeds
$totFeeds = $ArchivePress->getCommentFeeds();

if($status == "active"){
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-comments.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$totFeeds["total"].')</span></a> |</li>
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-comments.php&amp;post_status=active">'.__("Active", ARCHIVEPRESS).' <span class="count">('.$totFeeds["active"].')</span></a></li>
        </ul>';
} else {
    echo '
        <ul class="subsubsub">
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-comments.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$totFeeds["total"].')</span></a> |</li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-comments.php&amp;post_status=active">'.__("Active", ARCHIVEPRESS).' <span class="count">('.$totFeeds["active"].')</span></a></li>
        </ul>';
}

// PAGINATION
$paging = "";
if(isset($_GET['paging'])){
    $paging = attribute_escape($_GET['paging']);
}
if($status == "active") {
    $items = count($ArchivePress->getFeeds(true, true, $search, $protocol, "", "yes"));
}else {
    $items = count($ArchivePress->getFeeds(false, true, $search, $protocol, "", "yes"));
}
$p = new pagination;
$p->items($items);
$p->limit(15); // Limit entries per page
$p->target(str_replace("&","&amp;",$_SERVER['REQUEST_URI']));
$p->currentPage($paging); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(2); //No. of page away from the current page

if($paging == "") {
    $p->page = 1;
} else {
    $p->page = $paging;
}

$p->prevT = "";
$p->nextT = "";
$p->displaying = __("Displaying", ARCHIVEPRESS);
$p->of = __("of", ARCHIVEPRESS);

// limit for query
$limit = " LIMIT " . ($p->page - 1) * $p->limit  . ", " . $p->limit;

echo'
        <p class="search-box">
            <label for="post-search-input" class="screen-reader-text">'.__("Search Feeds", ARCHIVEPRESS).':</label>
            <input type="text" value="'.$ArchivePress->cleanOutput($search).'" name="s" id="post-search-input" />
            <input type="submit" class="button" value="'.__("Search Feeds", ARCHIVEPRESS).'" />
        </p>

        <div class="tablenav">

            <div class="alignleft actions">
                <select name="action">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="activatefeed">'.__("Activate", ARCHIVEPRESS).'</option>
                    <option value="deactivatefeed">'.__("Deactivate", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction" name="doaction" value="Apply" />
                <select class="postform" name="parentblog">
                    <option value="ALL"'; if($parentblog == "") echo ' selected="selected"'; echo'>'.__("View all Blogs", ARCHIVEPRESS).'</option>
                    '.$optionBlogs.'
                </select>
                <select class="postform" name="protocol">
                    <option value="ALL"'; if($protocol == "") echo ' selected="selected"'; echo'>'.__("View all protocols", ARCHIVEPRESS).'</option>
                    <option value="'.ArchivePressOptions::FEED_PROTOCOL_ATOM.'"'; if($protocol == ArchivePressOptions::FEED_PROTOCOL_ATOM) echo ' selected="selected"'; echo'>'.__("Atom", ARCHIVEPRESS).'</option>
                    <option value="'.ArchivePressOptions::FEED_PROTOCOL_RSS.'"'; if($protocol == ArchivePressOptions::FEED_PROTOCOL_RSS) echo ' selected="selected"'; echo'>'.__("Rss", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary" value="Filter" id="post-query-submit" />
            </div>

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="clear"></div>
        </div>

        <div class="clear"></div>

        <table cellspacing="0" class="widefat post fixed">
            <thead>
                <tr>
                    <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_name" id="name" scope="col">'.__("Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_url" id="url" scope="col">'.__("Feed Url", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedblog" id="feedblog" scope="col">'.__("Blog", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedposts" id="feedposts" scope="col"><div class="vers"><img src="images/comment-grey-bubble.png" alt="Comments" /></div></th>
                    <th style="" class="manage-column column-arpr_lastfetch" id="lastfetch" scope="col">'.__("Last fecth", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_nextfetch" id="nextfetch" scope="col">'.__("Next fetch", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_info" id="info" scope="col">'.__("Other Info", ARCHIVEPRESS).'</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th style="" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_name" scope="col">'.__("Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_url" scope="col">'.__("Feed Url", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedblog" scope="col">'.__("Blog", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedposts" scope="col"><div class="vers"><img src="images/comment-grey-bubble.png" alt="Comments" /></div></th>
                    <th style="" class="manage-column column-arpr_lastfetch" scope="col">'.__("Last fecth", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_nextfetch" scope="col">'.__("Next fetch", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_info" scope="col">'.__("Other Info", ARCHIVEPRESS).'</th>
                </tr>
            </tfoot>

            <tbody>';

// get feeds
$activeFeeds = array();
if($status == "active") {
    $activeFeeds = $ArchivePress->getFeeds(true, false, $search, $protocol, $limit, "yes");
}else {
    $activeFeeds = $ArchivePress->getFeeds(false, false, $search, $protocol, $limit, "yes");
}

// print feeds
if(count($activeFeeds) > 0){
    foreach($activeFeeds as $feed){
        // parent blog
        $blog = $ArchivePress->getParentBlogForFeed((int)$feed["id"]);

        if( ($parentblog == "") || ($parentblog == $blog["id"]) )
        {
            // last and next fetching date and time
            $lastFetch = $ArchivePress->getLastFetchedForFeed($feed["id"], "comment");
            $nextFetch = $ArchivePress->getNextToFetchForFeed($feed["id"], "comment");
            // if the feed is not active, DON'T print the next fetching time
            // and activate or deactivate links
            if( ($feed["active"] == "yes") && ($blog["active"] == "yes") ){
                $nextOutput = date('F jS, Y @ H:i',$nextFetch["datetime"]);
                $activate_deactivate = '<span class="edit"><a title="'.__("Deactivate this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deactivatecommentfeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . $feed["id"]).'">'.__("Deactivate", ARCHIVEPRESS).' </a> | </span>';
            } else {
                $nextOutput = __("Inactive", ARCHIVEPRESS);
                if($blog["active"] == "no"){
                   $nextOutput .= " (".__("Because the Blog is Inactive", ARCHIVEPRESS).")";
                }
                $activate_deactivate = '<span class="edit"><a title="'.__("Activate this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=activatecommentfeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . $feed["id"]).'">'.__("Activate", ARCHIVEPRESS).' </a> | </span>';
            }

            echo'
                    <tr valign="top" class="alternate iedit" id="feed-'.$feed["id"].'">
                        <th class="check-column" scope="row"><input type="checkbox" value="'.$feed["id"].'" name="feed[]" /></th>
                        <td class="column-arpr_name"><strong><a title="'.__("Edit", ARCHIVEPRESS).' '.$ArchivePress->cleanOutput($feed["name"]).'" href="https://da.ulcc.ac.uk/archivepress/wp-admin/post.php?action=edit&amp;post=63" class="row-title">'.$ArchivePress->cleanOutput($feed["name"]).'</a></strong>
                            <div class="row-actions">
                                <span class="edit"><a title="'.__("Edit this feed", ARCHIVEPRESS).'" href="admin.php?page=archivepress/pages/edit-comment.php&amp;feed_id='.$feed["id"].'">'.__("Edit", ARCHIVEPRESS).' </a> | </span>
                                '.$activate_deactivate.'
                                <span class="edit"><a title="'.__("Update this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=updatecommentfeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'update_commentfeed_' . $feed["id"]).'">'.__("Update", ARCHIVEPRESS).' </a></span>
                            </div>
                        </td>
                        <td class="column-arpr_url"><a title="'.$ArchivePress->cleanOutput($feed["name"]).'" href="'.$ArchivePress->cleanOutput($feed["url"]).'">'.$ArchivePress->cleanOutput($feed["url"]).'</a></td>
                        <td class="column-arpr_feedblog">
                                <a title="'.$ArchivePress->cleanOutput($blog["name"]).'" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/?page=archivepress/pages/edit-posts.php&amp;s='.$ArchivePress->cleanOutput($blog["name"]).'">
                                    '.$ArchivePress->cleanOutput($blog["name"]).'
                                </a>
                        </td>
                        <td class="column-arpr_feedposts">
                            <div class="post-com-count-wrapper">
                                <a class="post-com-count" title="'.__("Comments", ARCHIVEPRESS).'" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/edit-comments.php?p='.$blog["post_id"].'">
                                    <span class="comment-count">'.$ArchivePress->getCommentCount($blog["post_id"]).'</span>
                                </a>
                            </div>
                        </td>
                        <td class="column-arpr_lastfetch">'.mysql2date('F jS, Y @ H:i',$lastFetch["datetime"]).'</td>
                        <td class="column-arpr_nextfetch">'.$nextOutput.'</td>
                        <td class="column-arpr_info">
                            '.__("Protocol", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed["protocol"]).'<br />
                            '.__("Interval", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed["interval"]).' s
                        </td>
                    </tr>';
        }
    }
} else {
    echo '<tr valign="top" class="alternate iedit" id="feed-0"><td colspan="8">'.__("No Comment Feed found", ARCHIVEPRESS).'</td></tr>';
}

echo'
            </tbody>
        </table>
        <div class="tablenav">

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="alignleft actions">
                <select name="action2">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="activatefeed">'.__("Activate", ARCHIVEPRESS).'</option>
                    <option value="deactivatefeed">'.__("Deactivate", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction2" name="doaction2" value="Apply" />
                <br class="clear" />
            </div>
            <br class="clear" />
        </div>
    </form>
    </div>';

?>